/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quan;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Point;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

/**
 *
 * @author Quannk
 */
public class QFrame extends JFrame {

    private QDesktopPane desktopPane;
    private QOverviewPanel overviewPanel;
    public static final double scale1 = 0.8;
    public static final double scale2 = 0.4;
    private Dimension dms = new Dimension(800, 600);
    private Dimension visibleDimension;
    private Point currentPoint = new Point(0, 0);
    private QScrollPane scrollPane;
    private JTree tree;

    public QFrame() throws HeadlessException {
        setTitle("QDemo");
        setSize(1600, 900);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        initDesktopPane();
        initOverviewPanel();
        initTree();
    }

    public static void main(String[] args) {
//        UIManager.put("InternalFrame.activeTitleBackground", new Color(0, 0, 0, 0));
//        UIManager.put("InternalFrame.inactiveTitleBackground", new Color(0, 0, 0, 0));
        QFrame frame = new QFrame();
        frame.setVisible(true);
    }

    private void autoResize() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                        int newWidth = desktopPane.getPreferredSize().width + 10 * i;
                        int newHeight = desktopPane.getPreferredSize().height + 10 * i;
                        desktopPane.setPreferredSize(new Dimension(newWidth, newHeight));
                        desktopPane.setSize(newWidth, newHeight);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(QFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        t.start();
    }

    private void initDesktopPane() {
        scrollPane = new QScrollPane();
        desktopPane = new QDesktopPane(scrollPane);
        desktopPane.setPreferredSize(dms);
        desktopPane.setBackground(Color.red);
        scrollPane.getViewport().add(desktopPane);
        scrollPane.setDesktopPane(desktopPane);
        scrollPane.setBounds(0, 0, 1024, 768);
        add(scrollPane);
    }

    private void initOverviewPanel() {
        overviewPanel = desktopPane.createOverview();
        overviewPanel.setLocation(1100, 0);
        desktopPane.setOverviewPanel(overviewPanel);
        overviewPanel.setBackground(Color.YELLOW);
        add(overviewPanel);
        overviewPanel.setVisibleRect(scrollPane.getViewport().getViewRect());

    }

    private void initTree() {
        tree = new JTree();
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode parent1 = new DefaultMutableTreeNode("Parent 1");
        DefaultMutableTreeNode parent2 = new DefaultMutableTreeNode("Parent 2");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
        DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child 3");
        DefaultMutableTreeNode child4 = new DefaultMutableTreeNode("Child 4");
        parent1.add(child1);
        parent1.add(child2);
        parent2.add(child3);
        parent2.add(child4);
        root.add(parent1);
        root.add(parent2);
        TreeModel model = new DefaultTreeModel(root);
        tree.setModel(model);
        JScrollPane jsp = new JScrollPane(tree);
        jsp.setBounds(1100, 500, 200, 300);
        tree.setDragEnabled(true);
        add(jsp);
    }
}
